<?
session_start();

$firstPhoto = false;
$lastPhoto = false;
$isIE = false;

require("../mysql.config.inc");
$mysqli = new mysqli($host,$username,$password,$db);
		
if(!isset($_GET['pid'])){
	$_GET['pid'] = 1;
}elseif($_GET['pid'] <= 1){
	$_GET['pid'] = 1;
	$firstPhoto = true;
}

if(isset($_GET['aid']) && $_SESSION['lastPage'] == "Albums"){
	$maxPidResult = $mysqli->query("SELECT MAX(pid) FROM Photos NATURAL JOIN AlbumIn WHERE aid = ".$_GET['aid']);
	$maxPid= $maxPidResult->fetch_array();
	
	$minPidResult = $mysqli->query("SELECT MIN(pid) FROM Photos NATURAL JOIN AlbumIn WHERE aid = ".$_GET['aid']);
	$minPid= $minPidResult->fetch_array();
	
}else{
	$maxPidResult = $mysqli->query("SELECT MAX(pid) FROM Photos");
	$maxPid= $maxPidResult->fetch_array();
	
	$minPidResult = $mysqli->query("SELECT MIN(pid) FROM Photos");
	$minPid= $maxPidResult->fetch_array();
}
//Checks to see if the GET id number is greater than the highest actual PhotoID and then
//sets it to the actual max if it is. 
if($_GET['pid'] >= $maxPid[0]){
	$_GET['pid'] = $maxPid[0];
	$lastPhoto = true;
}elseif($_GET['pid'] <= $minPid[0]){
	$_GET['pid'] = $minPid[0];
	$firstPhoto = true;
}

if(isset($_GET['aid']) && $_SESSION['lastPage'] == "Albums"){
	$result = $mysqli->query("SELECT pid FROM Photos NATURAL JOIN AlbumIn WHERE aid =".$_GET['aid']);
}else{
	$result = $mysqli->query("SELECT pid FROM Photos");
}
while($pidArrayLine = $result->fetch_assoc()){
	$pidArray[] = $pidArrayLine['pid'];
}

if(!in_array($_GET['pid'],$pidArray)){
	getPid();
}

//If the GET id isn't an actual id but isn't over or under the highest one, adds
//1 or subtracts 1 until it is in the array.
function getPid(){
	require("../mysql.config.inc");
	$mysqli = new mysqli($host,$username,$password,$db);
	$result = $mysqli->query("SELECT pid FROM Photos");
	$mysqli->close();
	
	while($pidArrayLine = $result->fetch_assoc()){
		$pidArray[] = $pidArrayLine['pid'];
	}
	
	if(in_array($_GET['pid'],$pidArray)){
		return;
	}elseif(!in_array($_GET['pid'],$pidArray) && isset($_GET['back'])){
		$_GET['pid'] = $_GET['pid'] - 1;
		getPid();
	}else{
		$_GET['pid'] = $_GET['pid'] + 1;
		getPid();
	}
}

$caption = "";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>A Photo Gallery</title>
	<link rel="stylesheet" type="text/css" href="stylesheets/styles.css"/>
	<script type="text/javascript" src="scripts/photoScroll.js"></script>
</head>

<body>
<div id="content">
	<div id="nav">
		<dl class="menu">
			<dd><a href="index.php">Home</a></dd>
			<dd><a href="albums.php">Albums</a></dd>
			<dd class = "active"><a href="photos.php">All Photos</a></dd>
			<dd><a href="admin.php">Administration</a></dd>
		</dl>
	</div>
	
	<h2>View Photo</h2>

	<div class="nextLinks">
	<?
		if(isset($_GET['aid'])){
			print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".($_GET['pid']-1)."&amp;aid=".$_GET['aid']."&amp;back=true\"
			id=\"previousPhoto1\" class = \"previousPhoto\">	Previous Photo</a>");
		}else{
			print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".($_GET['pid']-1)."&amp;back=true\"
			id=\"previousPhoto1\" class = \"previousPhoto\">	Previous Photo</a>");
		}
			
		if(isset($_GET['aid'])){
			print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".($_GET['pid']+1)."&amp;aid=".$_GET['aid']."\"
			id=\"nextPhoto1\" class=\"nextPhoto\">Next Photo</a><br/><br/>");
		}else{
			print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".($_GET['pid']+1)."\"
			id=\"nextPhoto1\" class=\"nextPhoto\">Next Photo</a><br/><br/>");
		}
	?>
	</div>
	
	<?
		$result = $mysqli->query("SELECT* FROM Photos WHERE pid = ".$_GET['pid']);
		while($photos = $result->fetch_assoc()){
			$link = $photos['Link'];
			$imageSize = getimagesize($link);
			$imageWidth = $imageSize[0];
			
			if($imageWidth < 950){
				print("<img src=\"".$photos['Link']."\" id=\"photo\" class = \"photoDisplay\" 
				alt=\"Photo Gallery\" width=\"$imageWidth\" title=\"".$photos['Caption']."\" />\n");
				print("<div id=\"printCaption\">".$photos['Caption']."</"."div>\n");
			}else{
				print("<img src=\"".$photos['Link']."\" id=\"photo\" class = \"viewPhoto\" alt=\"Photo Gallery\"
				width=\"950\" title=\"".$photos['Caption']."\" />\n");
				print("<div id=\"printCaption\">".$photos['Caption']."</"."div>\n");
			}
		}
		
		print"<br/><br/>";
	?>	
	
	<div class="nextLinks">
		<?
		if(isset($_GET['aid'])){
			print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".($_GET['pid']-1)."&amp;aid=".$_GET['aid']."&amp;back=true\"
			id=\"previousPhoto2\" class = \"previousPhoto\">	Previous Photo</a>");
		}else{
			print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".($_GET['pid']-1)."&amp;back=true\"
			id=\"previousPhoto2\" class = \"previousPhoto\">	Previous Photo</a>");
		}
		
		if(isset($_GET['aid'])){
			print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".($_GET['pid']+1)."&amp;aid=".$_GET['aid']."\"
			id=\"nextPhoto2\" class=\"nextPhoto\">Next Photo</a><br/><br/>");
		}else{
			print("<a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/viewphoto.php?pid=".($_GET['pid']+1)."\"
			id=\"nextPhoto2\" class=\"nextPhoto\">Next Photo</a><br/><br/>");
		}
		
		
		if(isset($_SESSION['lastPage']) && $_SESSION['lastPage'] == "Albums" && isset($_GET['aid'])){
			print("<br/><a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/albums.php?albumId=".$_GET['aid']."\">Back to Albums</a>");
		}elseif(isset($_SESSION['lastPage']) && $_SESSION['lastPage'] == "Albums"){
			print("<br/><a href=\"albums.php\">Back to Albums</a>");
		}else{
			print("<br/><a href=\"photos.php\">Back to Gallery</a>");
		}
		
		if(isset($_SESSION['logged_user'])){
			echo("<br/><br/><a href=\"http://info230.cs.cornell.edu/users/aballantine/www/p3/admin/alterPhoto.php?pid=".($_GET['pid'])."\" id=\"alterPhotoLink\">Alter this photo&#39;s information</a>");
		}
		?>
		
		<form action="viewPhoto.php" method="post">
		<p>
			<input type="hidden" id="getPhotoId" value="<?print("".$_GET['pid']+1);?>"/>
			<input type="hidden" id="previousPhotoId" value="<?print("".$_GET['pid']-1);?>"/>
			<?if(isset($_GET['aid'])){?>
			<input type="hidden" id="getAlbumId" value="<?print("".$_GET['aid']);?>"/>
			<?}?>
		</p>
		</form>
		<?$mysqli->close();?>
	</div>
</div>
</body>
</html>